package cn.bestick.weightrecord.dao;

import cn.bestick.weightrecord.model.StudyRecord;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * Record: Bestick
 * Date: 2016/9/2
 * Time: 06:29
 * Url: http://www.bestick.cn
 * Copyright © 2015-2016 Bestick All rights reserved
 * -----------------------------------------------------------
 * 会当凌绝顶，一览众山小。
 */
@Component
public class StudyRecordDao extends BaseDao {

    @Value("${sql_ck_add_s_record}")
    private String addStudyRecord;

    @Value("${sql_ck_select_s_records_by_userId}")
    private String selectStudyRecordsByUserId;

    @Value("${sql_ck_select_s_record_by_date}")
    private String selectStudyRecordByDate;

    /**
     * 增加一条记录
     *
     * @param studyRecord 入参
     * @throws Exception
     */
    public void addStudyRecord(StudyRecord studyRecord) throws Exception {
        con = getCon();
        PreparedStatement pstmt = con.prepareStatement(addStudyRecord);
        pstmt.setString(1, studyRecord.getStudyGetupTime());
        pstmt.setString(2, studyRecord.getStudyGo2bedTime());
        pstmt.setString(3, studyRecord.getStudyTodaySum());
        pstmt.setInt(4, studyRecord.getUserId());
        pstmt.executeUpdate();
        close(con);
    }

    /**
     * 通过userId获取List<StudyRecord>
     *
     * @param userId 用户id
     * @return List<Record>
     * @throws Exception
     */
    public List<StudyRecord> getStudyRecordsByUserId(Integer userId) throws Exception {
        List<StudyRecord> records = new ArrayList<>();
        StudyRecord record;
        con = getCon();
        PreparedStatement pstmt = con.prepareStatement(selectStudyRecordsByUserId);
        pstmt.setInt(1, userId);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            record = new StudyRecord();
            record.setStudyRecordId(rs.getInt("s_recordId"));
            record.setUserId(rs.getInt("userId"));
            record.setStudyGetupTime(rs.getString("s_getupTime"));
            record.setStudyGo2bedTime(rs.getString("s_go2bedTime"));
            record.setStudyTodaySum(rs.getString("s_todaySum"));
            record.setStudyRecordTime(rs.getTimestamp("s_recordTime"));
            records.add(record);
        }
        close(con);
        return records;
    }

    /**
     * 获取今天的记录
     * @param today 指定日期为今天
     * @return 今天有记录true，无记录为false
     * @throws Exception
     */
    public boolean getStudyRecordByDate(String today, Integer userId) throws Exception {
        con = getCon();
        PreparedStatement pstmt = con.prepareStatement(selectStudyRecordByDate);
        pstmt.setString(1, today);
        pstmt.setInt(2, userId);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            close(con);
            return true;
        }
        close(con);
        return false;
    }
}
